package org.mycub.server.parser;

import org.json.JSONException;
import org.json.JSONObject;
import org.mycub.server.data.models.FreeParking;
import org.mycub.server.datainserter.FreeParkingsInserter;
import org.mycub.server.datainserter.factory.FreeParkingsInserterFactory;
import org.opengis.feature.simple.SimpleFeature;



public class CollectFreeParking extends CollectItem{

	FreeParking parking;

	public CollectFreeParking() {
		this.setFreeParking(new FreeParking());
	}

	public void collect(JSONObject json) {
		
		FreeParking parking = new FreeParking();
		
		try {
			String id = json.get("cle").toString();
			String nom = json.get("nom").toString();
			String nbPlaces = json.get("nombre_de_places").toString();
			String lon = json.get("x_long").toString();
			String lat = json.get("y_lat").toString();
			
			parking.setId(id);
			parking.setName(nom);
			parking.setPlaces((nbPlaces.isEmpty())?0:Integer.valueOf(nbPlaces));
			parking.setLongitude((lon.isEmpty())?0:Double.valueOf(lon));
			parking.setLatitude((lat.isEmpty())?0:Double.valueOf(lat));
			
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		System.out.println("collected free parking : " + parking.getName() + 
				" id = " + parking.getId());

		saveFreeParking(parking);
	}
	
	public void saveFreeParking(FreeParking freeParkingCollected){
		FreeParkingsInserter freeParkingsInserter = new FreeParkingsInserterFactory().createFreeParkingsInserter();
		freeParkingsInserter.save(freeParkingCollected);
	}

	public FreeParking getFreeParking() {
		return parking;
	}

	public void setFreeParking(FreeParking parking) {
		this.parking = parking;
	}

	@Override
	public void collect(SimpleFeature feature) {

	}	
}
